﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using VectorDraw.Professional.vdCollections;
using VectorDraw.Professional.vdObjects;
using VectorDraw.Professional.vdPrimaries;

namespace ZhCAD
{
    public partial class Childform : Form
    {
        private vdSelection _mSelection;
        public string ActiveFile = "";
        public FileTree Filelist;

        public IMainform Mainform;

        public Childform(string[] files, IMainform parentform)
        {
            InitializeComponent();
            Filelist = new FileTree();
            Mainform = parentform;
            DocHelper.MyEvent += DocHelper_MyEvent;
            vdScrollableControl1.BaseControl.SetActiveDocument(DocHelper.OpenDocument(files, Filelist));
            Document.OnFigureMouseOver += Document_OnFigureMouseOver;
            Document.OnAfterAddItem += Document_OnAfterAddItem;
            Document.OnBeforeModifyObject += Document_OnBeforeModifyObject;
            Document.OnEraseObject += Document_OnEraseObject;
            vdScrollableControl1.BaseControl.GripSelectionModified += BaseControl_GripSelectionModified;
            vdScrollableControl1.BaseControl.MouseMoveAfter += BaseControl_MouseMoveAfter;
            vdScrollableControl1.BaseControl.vdKeyDown += BaseControl_vdKeyDown;
            vdScrollableControl1.BaseControl.SelectionsAddItem += BaseControl_SelectionsAddItem;

           
            BshowProperty = true;

            if (files.Length == 0)
            {
                Text = "未命名";
            }
            else
            {
                Text = Path.GetFileName(files[0]) + (files.Length > 1 ? "多文档" : "");
                ActiveFile = files[0];
            }
        }

        private void DocHelper_MyEvent(object sender, long percent, string jobDescription)
        {
            Mainform.ShowProgress(sender,percent,jobDescription);
        }

        public vdDocument Document => vdScrollableControl1.BaseControl.ActiveDocument;

        public bool BshowProperty { get; set; }

        private void Document_OnBeforeModifyObject(object sender, string propertyname, object newvalue, ref bool cancel)
        {
//modify

            switch (propertyname)
            {
                case "Layer":
                case "Xproperty":
                    break;
            }


            switch (sender.GetType().Name)
            {
                case "vdLayer":
                    break;
                case "vdFigure":
                    break;
            }
        }

        private void Document_OnEraseObject(object sender, ref bool cancel)
        {
//delete
            var needUpdate = false;
            switch (sender.GetType().Name)
            {
                case "vdLayer":
                    break;
                case "vdLine":
                case "vdFigure":
                case "vdPolyline":
                case "vdText":
                case "vdInsert":
                case "vdPoint":
                    needUpdate = true;
                    DocHelper.DeleteEntFormFileTree(((vdFigure) sender).Id, Filelist);
                    break;
            }
            if (needUpdate & BshowProperty) Mainform.UpdateFileTree(null);
        }


        private void Document_OnAfterAddItem(object obj)
        {
//add
            var needUpdate = false;
            switch (obj.GetType().Name)
            {
                case "vdLayer":
                    break;
                case "vdLine":
                case "vdFigure":
                case "vdPolyline":
                    needUpdate = true;
                    DocHelper.AddEntToFileTree(ActiveFile,(vdFigure) obj, Filelist);
                    break;
            }
            if (needUpdate) Mainform.UpdateFileTree(null);
        }

        private void Document_OnFigureMouseOver(object sender, vdFigure.FigureMouseEventArgs args)
        {
            switch (args.MouseAction)
            {
                case vdFigure.FigureMouseEventArgs.FigureMouseAction.MouseEnter:
                    args.Entity.ToolTip = GetCass(args.Entity.Id);
                    break;
                case vdFigure.FigureMouseEventArgs.FigureMouseAction.MouseLeave:
                    args.Entity.ToolTip = "";
                    break;
            }
        }

        private void BaseControl_SelectionsAddItem(vdSelections sender, vdSelection item, ref bool cancel)
        {
            _mSelection = item;
            _mSelection.OnAddItem += MSelection_OnAddItem;
            _mSelection.OnRemoveItem += MSelection_OnRemoveItem;
        }


        private void MSelection_OnRemoveItem(object sender, object item)
        {
            ((vdFigure) item).HighLight = false;
            ((vdFigure) item).Invalidate();
        }

        private void MSelection_OnAddItem(object sender, object item, ref bool cancel)
        {
            ((vdFigure) item).HighLight = true;
            ((vdFigure) item).Invalidate();
        }

        private void BaseControl_vdKeyDown(KeyEventArgs e, ref bool cancel)
        {
            var overAllActiveAction = Document.ActiveLayOut.OverAllActiveAction;
            if (!overAllActiveAction.SendKeyEvents)
                return;

            var commandLine = Mainform.CommandLine;
            if (!commandLine.Visible)
                return;
            if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
                return;
            if (e.KeyCode == Keys.Delete && commandLine.UserTextString == "")
            {
                var sectionClips = Document.GetGripSelection();
                if (sectionClips.Count != 0)
                {
                    BshowProperty = false;
                    foreach (vdFigure figure in sectionClips)
                        figure.Deleted = true;

                    sectionClips.RemoveAll();
                    Document.Redraw(false);
                    Mainform.SelectObject(Document);
                    BshowProperty = true;
                    Mainform.UpdateFileTree(null);
                }
                else
                {
                    Mainform.CommandEx("erase");
                }
            }
            else
            {
                var message = default(Message);
                message.HWnd = commandLine.Handle;
                message.Msg = 256;

                message.WParam = (IntPtr) (long) e.KeyCode;
                commandLine.vdProcessKeyMessage(ref message);
            }
        }

        private void BaseControl_MouseMoveAfter(MouseEventArgs e)
        {
            var gPoint = Document.CCS_CursorPos();
            var x = gPoint.x;
            var y = gPoint.y;
            var z = gPoint.z;
            var text = string.Concat(Document.lunits.FormatLength(x), " , ", Document.lunits.FormatLength(y), " , ",
                Document.lunits.FormatLength(z));
            //if (Mainform != null && Mainform.mDisplayPolarCoord && Document.ActiveLayOut.OverAllActiveActions != null && Document.ActiveLayOut.OverAllActiveActions.Count > 1)
            //{
            //    BaseAction overAllActiveAction = Document.ActiveLayOut.OverAllActiveAction;
            //    if (overAllActiveAction.WaitingPoint && overAllActiveAction.ReferencePoint != null && (overAllActiveAction.ValueTypeProp & BaseAction.valueType.REFPOINT) == BaseAction.valueType.REFPOINT)
            //    {
            //        gPoint gPoint2 = Document.World2UserMatrix.Transform(overAllActiveAction.ReferencePoint);
            //        double angle = Globals.GetAngle(gPoint2, gPoint);
            //        double length2 = gPoint.Distance3D(gPoint2);
            //        y = Globals.RadiansToDegrees(angle);
            //        text = Document.lunits.FormatLength(length2) + "<" + Document.aunits.FormatAngle(angle);
            //    }
            //}
            Mainform.ShowCoordinate(text);
        }

        private string GetCass(int id)
        {

                var feat = Filelist.GetfeatureById(id);
                if (feat != null)
                {
                    if (feat.Cass != "")
                        return $"编码:{feat.Cass}\r\n说明:{DocHelper.GetCassDesc(feat.Cass)}";
                }
         
            return "";
        }

        private void BaseControl_GripSelectionModified(object sender, vdLayout layout, vdSelection gripSelection)
        {
            if (BshowProperty)
                if (gripSelection.Count == 0)
                    Mainform.SelectObject(Document);
                else
                    Mainform.SelectObject(gripSelection);
        }
    }
}